package com.dongdongshop.Controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ConstantEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TempLateService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("itemController")
public class ItemCatController {

    @Reference
    private ItemCatService itemCatService;
    @Reference
    private TempLateService tempLateService;


    @RequestMapping("toItemCat")
    public String toItemCat(Model model){

        List<TbTypeTemplate> tempList = tempLateService.selectAll();
        model.addAttribute("tempList",tempList);
        return "admin/item_cat";
    }

    @RequestMapping("findAll")
    @ResponseBody
    public Result findAll(Long parentId){

        List<TbItemCat> catList = itemCatService.findAll(parentId);

        return Result.build(ConstantEnum.SUCCESS).setResult(catList);
    }

    @RequestMapping("addInfo")
    @ResponseBody
    public Result addInfo(TbItemCat tbItemCat){
       boolean b = itemCatService.insert(tbItemCat);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("selectById")
    @ResponseBody
    public Result selectById(Long id){
        TbItemCat tbItemCat = itemCatService.selectById(id);
        return Result.build(ConstantEnum.SUCCESS).setResult(tbItemCat);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbItemCat tbItemCat){
        boolean b = itemCatService.updateInfo(tbItemCat);
        return Result.build(ConstantEnum.SUCCESS);
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Result deleteInfo(Long id){
        TbItemCat tbItemCat = itemCatService.selectById(id);
        return Result.build(ConstantEnum.SUCCESS);
    }
}
